 <template>
  <div>
    <el-row>
      <!--检索-->
      <el-form :inline="true" :model="search" class="demo-form-inline">
        <el-input v-if="false" v-model="search.pageNo"></el-input>
        <el-input v-if="false" v-model="search.pageSize"></el-input>
        <el-col :span="4">
          <el-input
            v-model="search.dictLabel"
            placeholder="请输入字典标签"
          ></el-input>
        </el-col>
        <el-col :span="4"
          ><div>
            <el-input
              v-model="search.dictType"
              placeholder="请输入字典类型"
            ></el-input></div
        ></el-col>
        <el-col :span="12"
          ><div>
            <el-button type="primary" icon="el-icon-search" @click="searchBtn"
              >搜索</el-button
            >
            <el-button type="info" icon="el-icon-loading" @click="emptyBtn"
              >刷新</el-button
            >
          </div>
        </el-col></el-form
      >
      <el-button
        type="primary"
        @click="addBtn"
        icon="el-icon-circle-plus"
        >新增</el-button
      >
      <!--功能列表-->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="#" sortable> </el-table-column>
        <el-table-column prop="dictLabel" label="字典标签">
        </el-table-column>
        <el-table-column prop="dictValue" label="字典值">
        </el-table-column>
        <el-table-column prop="dictName" label="字典类名">
        </el-table-column>
        <el-table-column prop="dictType" label="字典类型">
        </el-table-column>
        <el-table-column prop="createBy" label="创建人">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180px">
        </el-table-column>
        
        <el-table-column
          prop="status"
          label="状态"
          :formatter="getStatusName"
        ></el-table-column>
        <el-table-column prop="remark" label="字典备注">
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              size="small"
              @click="updateRole(scope.$index, scope.row)"
              icon="el-icon-edit"
              type="success"
              >编辑</el-button
            >
            <el-button
              size="small"
              @click="deleteRole(scope.$index, scope.row)"
              icon="el-icon-delete"
              type="danger"
              >删除</el-button
            >
            
          </template>
        </el-table-column>
      </el-table>
      <!--分页功能-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[1,5, 10, 20, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
      <!--新增/修改 Form -->
      <el-dialog title="字典更新" :visible.sync="editFormState">
        <el-form :model="form">
          <el-form-item label="字典标签" :label-width="formLabelWidth">
            <el-input
              v-model="form.dictLabel"
              placeholder="字典标签"
              auto-complete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="字典值" :label-width="formLabelWidth">
            <el-input
              v-model="form.dictValue"
              placeholder="字典值"
              auto-complete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="字典类名" :label-width="formLabelWidth">
            <el-input
              v-model="form.dictName"
              placeholder="字典类名"
              auto-complete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="字典类型" :label-width="formLabelWidth">
            <el-input
              v-model="form.dictType"
              placeholder="字典类型"
              auto-complete="off"
            ></el-input>
          </el-form-item>
           <el-form-item label="字典备注" :label-width="formLabelWidth">
            <el-input
              v-model="form.remark"
              placeholder="字典备注"
              auto-complete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editFormState = false">取 消</el-button>
          <el-button type="primary" @click="saveBtn">确 定</el-button>
        </div>
      </el-dialog>
    
    </el-row>
  </div>
</template>

<script>
import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
Vue.use(VueAxios, axios);

export default {
  data() {
    return {
      tableData: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      page: {},
      form: { status: "0" },
      search: { pageNo: 1, pageSize: 5 },
      formLabelWidth: "80px",
      editFormState: false,
      menuTreeVisible: false
    };
  },
  created: function () {
    this.load();
  },
  methods: {
    //初始化数据
    load() {
      var _this = this;
      //页面初始化时，加载的方法
      this.$http  
        .get("http://192.168.1.72:9999/cms/dictData/queryAllByParam", {
          params: {
            pageNo: this.search.pageNo,
            pageSize: this.search.pageSize,
            dictLabel: this.search.dictLabel,
            dictType: this.search.dictType,
          },
        })
        .then((response) => {
          console.log(response.data);
          _this.tableData = response.data.data.list;
          _this.page = response.data.data;
          _this.total = response.data.data.total;
        });
    },
    //每页显示数量发生变化
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.search.pageSize = val;
      this.load();
    },
    //第几页发生变化
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.search.pageNo = val;
      this.load();
    },
    //查询
    searchBtn() {
      console.log(`当前页`);
      this.load();
    },
    emptyBtn() {
      this.search = {};
      this.search.pageNo = 1;
      this.search.pageSize = 5;
      this.load();
    },
    getStatusName(row, column, cellValue) {
      return cellValue == 0 ? "启用" : "禁用";
    },
    updateRole: function (index, row) {
      this.form = row;
      this.editFormState = true;
    },
    deleteRole: function (index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var _this = this;
          axios
            .delete(
              "http://localhost:9999/cms/dictData/deleteById?dictCode=" + row.dictCode
            )
            .then(function (response) {
              console.log(response);
              if (response.status == 200) {
                  //重新加载数据
                  _this.load();
                  //提示成功
                  _this.$message({
                    type: "success",
                    message: "删除成功!",
                  });
                  
              }
            })
            .catch(function (error) {
              console.log(error);
              _this.$message.error("你错了。。。");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addRoleAndMenus() {
      //获取重新选中的节点
      let nodes = this.$refs.menuTree.getCheckedNodes();
      //判断至少选中一个节点
      if (nodes.length < 1) {
        this.$message.error("至少选中一个节点");
        return;
      }
      let tempMenuIds = "";
      //循环遍历
      for (var i = 0; i < nodes.length; i++) {
        tempMenuIds += nodes[i].id + ",";
      }
      tempMenuIds = tempMenuIds.substr(0, tempMenuIds.length - 1);
      console.log(
        "当前字典为：" +
          this.roleId +
          "，该字典对应新的权限ID为：" +
          tempMenuIds
      );
      var _this = this;
      //执行后台添加方法
      axios
        .post(
          "http://localhost:9999/prs/role/addRoleAndMenu?roleId=" +
            this.roleId +
            "&menuIds=" +
            tempMenuIds
        )
        .then(function (response) {
          console.log(response);
          if (response.status == 200) {
            if (response.data > 0) {
              //提示成功
              _this.$message({
                type: "success",
                message: "授权成功!",
              });
              //关闭弹框
              _this.menuTreeVisible = false;
              //重新加载数据
              _this.load();
            }
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    saveBtn() {
      var _this = this;
      if (
        this.form.dictCode != null &&
        this.form.dictCode != "" &&
        this.form.dictCode != undefined
      ) {
        axios
          .put("http://localhost:9999/cms/dictData/update", this.form)
          .then(function (response) {
            //提示成功
                  _this.$message({
                    type: "success",
                    message: "修改成功!",
                  });
            //正确请求时，回调函数
            console.log(response);
            _this.load();
            _this.editFormState = false;
          })
          .catch(function (error) {
            _this.$message.error("你错了。。。");
            _this.editFormState = false;
            _this.load();
            //失败处理
            console.log(error);
          });
      } else {
        axios
          .post("http://localhost:9999/cms/dictData/insert", this.form)
          .then(function (response) {
            //提示成功
                  _this.$message({
                    type: "success",
                    message: "新增成功!",
                  });
            //正确请求时，回调函数
            console.log(response);
            _this.load();
            _this.editFormState = false;
          })
          .catch(function (error) {
            _this.$message.error("你错了。。。");
            //失败处理
            console.log(error);
          });
      }
    },
    addBtn() {
      this.form = {};
      this.editFormState = true;
    }
  },
};
</script>
<style>
</style>